﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;


namespace StudentManagement
{
    public partial class frmCourseM : Form
    {
        public frmCourseM()
        {
            InitializeComponent();
        }

        private void tianjia_Click(object sender, EventArgs e)
        {
            frmkecheng f = new frmkecheng();//打开窗口
            DialogResult dr = f.ShowDialog();
            if (dr==DialogResult.OK)
            {
                BindingData();   //如果添加成功，重新读取数据库
            }
            
            
        }
        public void BindingData()
        {
            //窗体显示之前执行的代码
            //读取数据库
            string connstr = "server=.;database=Class;user=sa;password=123456";
            SqlConnection conn = new SqlConnection(connstr);
            conn.Open();//打开数据库
            string selectsql = "SELECT Id,Name FROM Subject";
            SqlCommand comm = new SqlCommand(selectsql, conn);//执行comm的方法，开始读取
            SqlDataReader dr = comm.ExecuteReader();
            List<StudentInfo> stuList = new List<StudentInfo>();//数据存储在泛型集合中List<StudentInfo>
            while (dr.Read())
            {
                StudentInfo s = new StudentInfo();
                s.Id = dr.GetString(0);
                s.Name = dr.GetString(1);
                stuList.Add(s);
            }//读取数据
            conn.Close();//关闭数据库
            this.kecheng.DataSource = stuList;//将读取到的数据显示在窗口上
        }

        private void frmCourseM_Load(object sender, EventArgs e)
        {
            BindingData();//读取数据库
        }

        private void shanchu_Click(object sender, EventArgs e)
        {
            string id = this.kecheng.SelectedRows[0].Cells[0].Value.ToString();//获取说选中行的信息
            string connstr = "server=.;database=Class;user=sa;password=123456";//连接数据库
            SqlConnection conn = new SqlConnection(connstr);
            conn.Open();//打开数据库
            string sql = "delete from subject where id="+id;
            SqlCommand comm = new SqlCommand(sql, conn);//执行comm的方法，开始读取
            int count =  comm.ExecuteNonQuery();//数据库中受影响的行数
            if (count > 0)
            {
                BindingData();//如果行数大于零 重新读取数据库
            }
            
        }

        private void kecheng_DoubleClick(object sender, EventArgs e)
        {
            //获取双击的行的数据
            DataGridViewRow dgvr = this.kecheng.SelectedRows[0];
            StudentInfo s = new StudentInfo();//打开窗口时保存原有的数据
            s.Id = dgvr.Cells[0].Value.ToString();
            s.Name = dgvr.Cells[1].Value.ToString();//获取值
            //打开编辑窗体
            frmkecheng f = new frmkecheng();
            f.StudentInfo = s;
            f.Text = "编辑课程信息";
            DialogResult dr = f.ShowDialog();
            if (dr==DialogResult.OK)
            {
                BindingData();//如果添加成功，重新读取数据库
            }

        }



        
    
            
    }
}
